package com.chixing.day10.day10_Homew;

public class MapSum {
    private keyValue[] kv;
    private int count = 0;

    public MapSum(){

    }
    public MapSum(int length){
        this.kv = new keyValue[length];
    }

    public keyValue[] getKv() {
        return kv;
    }

    public void setKv(keyValue[] kv) {
        this.kv = kv;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void insert(String key, int val){
        int flag = 0;
        for (int i = 0; i < count; i++){
            if (key == kv[i].getKey()){
                flag = 1;
                keyValue kv1 = new keyValue(key, val);
                kv[i] = kv1;
            }
        }
        if (flag == 0 || count == 0){
            keyValue kv1 = new keyValue(key, val);
            kv[count++] = kv1;
        }
    }

    public int sum(String prefix){
        int sum = 0;
        for (int i = 0; i <this.count;i++){
            if(kv[i].getKey().startsWith(prefix)){
                sum += kv[i].getValue();
            }
        }
     return sum;
    }
}
